package com.bruce.ai.alibaba.chat.memory.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.alibaba.cloud.ai.memory.jdbc.MysqlChatMemoryRepository;
import com.alibaba.cloud.ai.memory.redis.RedissonRedisChatMemoryRepository;
import com.bruce.ai.alibaba.chat.memory.properties.MysqlMemoryProperties;
import com.bruce.ai.alibaba.chat.memory.properties.RedisMemoryProperties;

@Configuration
@EnableConfigurationProperties({RedisMemoryProperties.class, MysqlMemoryProperties.class})
public class MemoryConfig {

    /**
     * 配置 MysqlChatMemoryRepository  Bean
     *
     * @param mysqlMemoryProperties MySQL 内存属性配置
     * @return MysqlChatMemoryRepository 实例
     */
    @Bean
	public MysqlChatMemoryRepository mysqlChatMemoryRepository(MysqlMemoryProperties mysqlMemoryProperties) {
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName(mysqlMemoryProperties.getDriverClassName());
		dataSource.setUrl(mysqlMemoryProperties.getJdbcUrl());
		dataSource.setUsername(mysqlMemoryProperties.getUsername());
		dataSource.setPassword(mysqlMemoryProperties.getPassword());
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		return MysqlChatMemoryRepository.mysqlBuilder()
				.jdbcTemplate(jdbcTemplate)
				.build();
	}


    /**
     * 配置 RedissonRedisChatMemoryRepository  Bean
     *
     * @param redisMemoryProperties Redis 内存属性配置
     * @return RedissonRedisChatMemoryRepository 实例
     */
    @Bean
    public RedissonRedisChatMemoryRepository redissonRedisChatMemoryRepository(RedisMemoryProperties redisMemoryProperties) {
        return RedissonRedisChatMemoryRepository.builder()
        .port(redisMemoryProperties.getPort())
        .host(redisMemoryProperties.getHost())
        .password(redisMemoryProperties.getPassword())
        .build();
    }

}
